import table from '@/components/dt/table/render.js'
import Opt from '@/components/dt/info/Opt.js'
import dateOpt from '@/biz/option/date.js'
import houseOpt from '@/biz/option/house.js'
import nameOpt from '@/biz/option/name.js'
import contractMgr from '@/biz/sales/contractOrder.js'
import dt from '@dt/dt'
let _this = null

class Column extends Opt {
  constructor(vm) {
    super()
    _this = vm
    this.build()
  }

  build() {
    this.createAll()
    this.opts.push(...this.all)
  }

  createAll() {
    console.log(_this.orderType, 'orderType')
    this.all = []
    let publicArr = [
      {
        title: `${_this.orderType}单编号`,
        field: 'orderCode',
        minWidth: 160,
        header: {
          filter: {
            title: `${_this.orderType}单编号`,
            key: 'orderCode',
            placeholder: `请输入至少4位${_this.orderType}单编号`,
            input: {},
            check: (val) => {
              if (val.orderCode.length < 4) {
                _this.$Message.warning(`请输入至少4位${_this.orderType}单编号。`)
              }
              return !(val.orderCode.length < 4)
            }
          }
        }
      },
      {
        field: 'roomName',
        minWidth: 130,
        header: {
          filter: houseOpt.createRoom({
            key: 'roomId',
            title: `${_this.orderType}房源`,
            buildingId: _this.$route.query.housesId
          })
        }
      },
      {
        field: 'customerName',
        minWidth: 150,
        slots: {
          default: (params, h) => {
            return table.cell.btn(_this, h, {
              title: params.row.customerName,
              event: 'onLinkUser',
              data: params.row
            })
          }
        },
        header: {
          filter: {
            title: '客户姓名',
            key: 'customerName',
            placeholder: '请输入客户姓名',
            input: {}
          }
        }
      },
      {
        title: '客户手机号',
        field: 'customerPhone',
        minWidth: 150,
        header: {
          filter: {
            title: '客户手机号',
            key: 'customerPhone',
            placeholder: '请输入至少4位客户手机号',
            input: {},
            check: (val) => {
              if (val.customerPhone.length < 4) {
                _this.$Message.warning('请输入至少4位客户手机号。')
              }
              return !(val.customerPhone.length < 4)
            }
          }
        }
      },
      {
        field: 'counselorName',
        minWidth: 130,
        header: {
          filter: nameOpt.createPushPeople({
            key: 'counselorId',
            title: '归属顾问',
            to: (info) => {
              return {
                counselorId: info.userId
              }
            }
          })
        }
      },
      {
        field: 'productName',
        title: '所属产品',
        minWidth: 150,
        cell: {
          text: {
            lineClamp: 2,
            webkitBoxOrient: 'vertical',
            display: '-webkit-box',
            whiteSpace: 'inherit'
          }
        }
      },
      {
        title: '当前面积(㎡)',
        field: 'currentAdjustArea',
        minWidth: 150
      },
      {
        title: `当前${_this.orderType}总价(元)`,
        field: 'currentAdjustPrice',
        minWidth: 140
      },
      {
        title: '调整后面积(㎡)',
        field: 'afterAdjustArea',
        minWidth: 150
      },
      {
        title: `调整后${_this.orderType}总价(元)`,
        field: 'afterAdjustPrice',
        minWidth: 150
      },
      {
        title: '调整面积差(㎡)',
        field: 'areaDV',
        minWidth: 150,
        slots: {
          default: (params, h) => {
            let color = params.row.areaDV > 0 ? '#D9001B' : params.row.areaDV < 0 ? '#70B603' : '#515a6e'

            return table.cell.text(h, {
              value: params.row.areaDV > 0 ? '+' + params.row.areaDV : params.row.areaDV,
              color: color
            })
          }
        }
      },
      {
        title: '调整总价差(元)',
        field: 'priceDV',
        minWidth: 150,
        slots: {
          default: (params, h) => {
            let color = params.row.priceDV > 0 ? '#D9001B' : params.row.priceDV < 0 ? '#70B603' : '#515a6e'

            return table.cell.text(h, {
              value: params.row.priceDV > 0 ? '+' + params.row.priceDV : params.row.priceDV,
              color: color
            })
          }
        }
      }
    ]

    let subArr = [
      {
        field: 'subscriptionDate',
        minWidth: 150,
        title: '认购日期',
        header: {
          filter: dateOpt.range({
            title: '认购日期',
            startKey: 'subscriptionStartDate',
            endKey: 'subscriptionEndDate',
            disabled: (date) => {
              return date.valueOf() > new Date().getTime()
            },
            check: (val) => {
              if (_this.$tool.dateDiff(val.subscriptionStartDate, val.subscriptionEndDate).Y >= 2) {
                // 日期最大跨度两年
                _this.$Message.warning('认购日期跨度至多两年。')
              }
              return !(_this.$tool.dateDiff(val.subscriptionStartDate, val.subscriptionEndDate).Y >= 2)
            }
          })
        }
      },
      {
        title: '认购人',
        field: 'contractorName',
        minWidth: 130
      },
      {
        title: '认购状态',
        field: 'showStatus',
        minWidth: 130,
        header: {
          filter: {
            title: '认购状态',
            key: 'showStatus',
            placeholder: '请选择认购状态',
            select: {
              options: dt.dictInfo().subscriptionStatus.filter((item) => {
                return item.key == 4 || item.key == 7
              }),
              multiple: true,
              maxTagCount: 1
            }
          }
        },
        cell: (item) => {
          return {
            text: {
              type: 'dict',
              key: 'subscriptionStatus',
              color: item.showStatus == 2 ? '#D9001B' : '#515a6e'
            }
          }
        }
      },
      {
        field: 'handles',
        title: '操作',
        width: 100,
        fixed: 'right',
        slots: {
          default: (params, h) => {
            let btns = []
            btns.push({
              title: '订单详情',
              event: 'orderDetailFun',
              data: params.row
            })
            return table.cell.btns(_this, h, btns)
          }
        }
      }
    ]
    let contractArr = [
      {
        title: '调整后新增回款金额(元)',
        field: 'priceDV',
        minWidth: 200,
        header: {
          Tooltip: {
            title: '调整后新增回款金额(元)',
            tipsTxt: '“签约总价”调整审核通过后，系统将根据“总价差”生成“回款计划”，并自动执行回款操作',
            icon: 'ios-alert-outline',
            iconStyle: {
              fontSize: '20px',
              marginRight: '6px',
              color: '#D9001B',
              cursor: 'pointer',
              fontWeight: 'bolder'
            }
          }
        },
        slots: {
          default: (params, h) => {
            let color = params.row.priceDV > 0 ? '#D9001B' : params.row.priceDV < 0 ? '#70B603' : '#515a6e'

            return table.cell.text(h, {
              value: params.row.priceDV > 0 ? '+' + params.row.priceDV : params.row.priceDV,
              color: color
            })
          }
        }
      },
      {
        title: '签约日期',
        field: 'contractDate',
        minWidth: 180,
        header: {
          filter: dateOpt.range({
            title: '签约日期',
            startKey: 'subscriptionStartDate',
            endKey: 'subscriptionEndDate',
            disabled: (date) => {
              return date.valueOf() > new Date().getTime()
            },
            check: (val) => {
              if (_this.$tool.dateDiff(val.subscriptionStartDate, val.subscriptionEndDate).Y >= 2) {
                // 日期最大跨度两年
                _this.$Message.warning('签约日期跨度至多两年。')
              }
              return !(_this.$tool.dateDiff(val.subscriptionStartDate, val.subscriptionEndDate).Y >= 2)
            }
          })
        }
      },
      {
        title: '签约人',
        field: 'contractorName',
        minWidth: 150,
        header: {
          filter: nameOpt.createContractor({
            key: 'contractorId',
            title: '签约人'
          })
        }
      },
      {
        title: '签约状态',
        field: 'showStatus',
        minWidth: 150,
        header: {
          filter: {
            title: '签约状态',
            key: 'showStatus',
            placeholder: '请选择签约状态',
            select: {
              // options: contractMgr.getContractStatus(),
              options: dt.dictInfo().status.filter((item) => {
                return item.key == 10104 || item.key == 10109 || item.key == 10108
              }),
              multiple: true,
              maxTagCount: 1
            }
          }
        },
        cell: (item) => {
          return {
            text: {
              type: 'dict',
              key: 'status',
              color: item.showStatus == 10102 ? '#D9001B' : '#515a6e'
            }
          }
        }
      },
      {
        field: 'handles',
        title: '操作',
        width: 100,
        fixed: 'right',
        slots: {
          default: (params, h) => {
            let btns = []
            btns.push({
              title: '订单详情',
              event: 'orderDetailFun',
              data: params.row
            })
            return table.cell.btns(_this, h, btns)
          }
        }
      }
    ]
    if (_this.orderType == '认购') {
      this.all.push(...publicArr, ...subArr)
    } else if (_this.orderType == '签约') {
      this.all.push(...publicArr, ...contractArr)
    }
  }
}

export default Column
